package GUI;


import DTO.MailStructureDTO;
import java.awt.event.ActionEvent;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.MessagingException;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.event.HyperlinkEvent;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
import DTO.*;
import BUS.*;
import UTIL.ConvertString;
import UTIL.ReadXML;
import java.awt.Button;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.util.ArrayList;
import java.util.Locale;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.event.HyperlinkListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
import sun.awt.CustomCursor;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainApp.java
 *
 * Created on May 15, 2009, 6:39:25 PM
 */

/**
 *
 * @author NxSiHMr0D
 */
public class MainApp extends javax.swing.JFrame {

    /** Creates new form MainApp */
    public MainApp() {
        initComponents();

    }
     MailFunctionBUS mailFuncBUS; //= new MailFunctionBUS();
     int rows;
     JPopupMenu Pmenu,Pmenu2;
     MailInBUS mailInBus;
     ArrayList<MailInDTO> listBox = new ArrayList<MailInDTO>();

    private void add2PopupMenu() {
        //throw new UnsupportedOperationException("Not yet implemented");
         JMenuItem menuItem;
        Pmenu = new JPopupMenu();
        menuItem = new JMenuItem("Mark as Unread");
        menuItem.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                try {
                    //throw new UnsupportedOperationException("Not supported yet.");
                    //JOptionPane.showMessageDialog(null, e.getActionCommand().toString());
                    mailFuncBUS.setMaskMail(rows, tbListMail, 1);
                } catch (SQLException ex) {
                    Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
        Pmenu.add(menuItem);
        menuItem = new JMenuItem("Mark as Read");
         menuItem.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                //throw new UnsupportedOperationException("Not supported yet.");
                //JOptionPane.showMessageDialog(null, e.getActionCommand().toString());
                 try {
                    //throw new UnsupportedOperationException("Not supported yet.");
                    //JOptionPane.showMessageDialog(null, e.getActionCommand().toString());
                    mailFuncBUS.setMaskMail(rows, tbListMail, 2);
                } catch (SQLException ex) {
                    Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
        Pmenu.add(menuItem);
        Pmenu.addSeparator();
        menuItem = new JMenuItem("Mark as Important");
         menuItem.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                //throw new UnsupportedOperationException("Not supported yet.");
                //JOptionPane.showMessageDialog(null, e.getActionCommand().toString());
                 try {
                    //throw new UnsupportedOperationException("Not supported yet.");
                    //JOptionPane.showMessageDialog(null, e.getActionCommand().toString());
                    mailFuncBUS.setMaskMail(rows, tbListMail, 3);
                } catch (SQLException ex) {
                    Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
        Pmenu.add(menuItem);
        menuItem = new JMenuItem("Mark as Personal");
         menuItem.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                //throw new UnsupportedOperationException("Not supported yet.");
                //JOptionPane.showMessageDialog(null, e.getActionCommand().toString());
                 try {
                    //throw new UnsupportedOperationException("Not supported yet.");
                    //JOptionPane.showMessageDialog(null, e.getActionCommand().toString());
                    mailFuncBUS.setMaskMail(rows, tbListMail, 4);
                } catch (SQLException ex) {
                    Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
        Pmenu.add(menuItem);
        Pmenu.addSeparator();
        
        JMenu move1 = new JMenu("Move to Folder");
         JMenu move2 = new JMenu("Move to MainIn");
        ArrayList<MailInDTO> lst;
        try {
            lst = mailInBus.getListBox();
            for (int index = 0; index < lst.size(); index++) {
                final MailInDTO ma = lst.get(index);
                JMenuItem tmp = new JMenuItem(ma.getNameIn());
                tmp.addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent e) {
                        try {
                            //throw new UnsupportedOperationException("Not supported yet.");
                            // JOptionPane.showMessageDialog(null, e.getActionCommand().toString());
                            //mailFuncBUS.setMaskMail(rows, tbListMail, 4);
                            mailInBus.setFolder(rows, tbListMail, ma.getIdMainIn());
                        } catch (SQLException ex) {
                            Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
                        }

                    }
                });
                if ( index <4){
                    move2.add(tmp);
                }
                else
                    move1.add(tmp);
            }
        } catch (SQLException ex) {
            Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
        }
        Pmenu.add(move1);
        Pmenu.add(move2);
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToolBar1 = new javax.swing.JToolBar();
        btGetMail = new javax.swing.JButton();
        btWriteMail = new javax.swing.JButton();
        btContact = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        btReply = new javax.swing.JButton();
        btConfig = new javax.swing.JButton();
        jSeparator4 = new javax.swing.JToolBar.Separator();
        btMarkMail = new javax.swing.JButton();
        btDeleteMail = new javax.swing.JButton();
        btCalendar = new javax.swing.JButton();
        btCalendar1 = new javax.swing.JButton();
        jSplitPane1 = new javax.swing.JSplitPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        trFolder = new javax.swing.JTree();
        jSplitPane2 = new javax.swing.JSplitPane();
        jPanel1 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tbListMail = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        jSplitPane3 = new javax.swing.JSplitPane();
        jScrollPane3 = new javax.swing.JScrollPane();
        txtContentMail = new javax.swing.JEditorPane();
        jScrollPane4 = new javax.swing.JScrollPane();
        jPanel4 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        cbChooseSearch = new javax.swing.JComboBox();
        txtSearch = new javax.swing.JTextField();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu4 = new javax.swing.JMenu();
        mnNewMail = new javax.swing.JMenuItem();
        jSeparator3 = new javax.swing.JSeparator();
        mnExit = new javax.swing.JMenuItem();
        jSeparator2 = new javax.swing.JSeparator();
        jMenu2 = new javax.swing.JMenu();
        mnGetMail = new javax.swing.JMenuItem();
        mnWriteMail = new javax.swing.JMenuItem();
        mnReplyMail = new javax.swing.JMenuItem();
        mnContact = new javax.swing.JMenuItem();
        mnConfig = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jToolBar1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED, null, java.awt.Color.black, null, null));
        jToolBar1.setBorderPainted(false);

        btGetMail.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Mail.png"))); // NOI18N
        btGetMail.setMnemonic('1');
        btGetMail.setText("Receive Mail");
        btGetMail.setToolTipText("Get mail form server and database");
        btGetMail.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        btGetMail.setFocusable(false);
        btGetMail.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btGetMail.setPreferredSize(new java.awt.Dimension(40, 73));
        btGetMail.setSelectedIcon(new javax.swing.ImageIcon(getClass().getResource("/Save.png"))); // NOI18N
        btGetMail.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btGetMail.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btGetMailActionPerformed(evt);
            }
        });
        jToolBar1.add(btGetMail);

        btWriteMail.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Send message.png"))); // NOI18N
        btWriteMail.setMnemonic('1');
        btWriteMail.setText("Send Mail");
        btWriteMail.setToolTipText("Send mail to another person");
        btWriteMail.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        btWriteMail.setFocusable(false);
        btWriteMail.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btWriteMail.setPreferredSize(new java.awt.Dimension(40, 73));
        btWriteMail.setSelectedIcon(new javax.swing.ImageIcon(getClass().getResource("/Save.png"))); // NOI18N
        btWriteMail.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btWriteMail.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btWriteMailActionPerformed(evt);
            }
        });
        jToolBar1.add(btWriteMail);

        btContact.setIcon(new javax.swing.ImageIcon(getClass().getResource("/contact.png"))); // NOI18N
        btContact.setMnemonic('1');
        btContact.setText("Contact");
        btContact.setToolTipText("Show all contacts");
        btContact.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        btContact.setFocusable(false);
        btContact.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btContact.setPreferredSize(new java.awt.Dimension(40, 73));
        btContact.setSelectedIcon(new javax.swing.ImageIcon(getClass().getResource("/Save.png"))); // NOI18N
        btContact.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btContact.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btContactActionPerformed(evt);
            }
        });
        jToolBar1.add(btContact);

        jSeparator1.setEnabled(false);
        jToolBar1.add(jSeparator1);

        btReply.setIcon(new javax.swing.ImageIcon(getClass().getResource("/mail_reply.png"))); // NOI18N
        btReply.setMnemonic('1');
        btReply.setText("Reply mail");
        btReply.setToolTipText("Reply mail to person who send mail to default account");
        btReply.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        btReply.setFocusable(false);
        btReply.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btReply.setPreferredSize(new java.awt.Dimension(40, 73));
        btReply.setSelectedIcon(new javax.swing.ImageIcon(getClass().getResource("/Save.png"))); // NOI18N
        btReply.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btReply.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btReplyActionPerformed(evt);
            }
        });
        jToolBar1.add(btReply);

        btConfig.setIcon(new javax.swing.ImageIcon(getClass().getResource("/wizard.png"))); // NOI18N
        btConfig.setMnemonic('1');
        btConfig.setText("  Wizard   ");
        btConfig.setToolTipText("Configurate the program");
        btConfig.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        btConfig.setFocusable(false);
        btConfig.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btConfig.setPreferredSize(new java.awt.Dimension(40, 73));
        btConfig.setSelectedIcon(new javax.swing.ImageIcon(getClass().getResource("/Save.png"))); // NOI18N
        btConfig.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btConfig.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btConfigActionPerformed(evt);
            }
        });
        jToolBar1.add(btConfig);
        jToolBar1.add(jSeparator4);

        btMarkMail.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tag-blue.png"))); // NOI18N
        btMarkMail.setMnemonic('1');
        btMarkMail.setText("  Mark  ");
        btMarkMail.setToolTipText("Mark mail");
        btMarkMail.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        btMarkMail.setFocusable(false);
        btMarkMail.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btMarkMail.setPreferredSize(new java.awt.Dimension(40, 73));
        btMarkMail.setSelectedIcon(new javax.swing.ImageIcon(getClass().getResource("/Save.png"))); // NOI18N
        btMarkMail.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btMarkMail.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btMarkMailActionPerformed(evt);
            }
        });
        jToolBar1.add(btMarkMail);

        btDeleteMail.setIcon(new javax.swing.ImageIcon(getClass().getResource("/delete.png"))); // NOI18N
        btDeleteMail.setMnemonic('1');
        btDeleteMail.setText("Delete");
        btDeleteMail.setToolTipText("Delete mail");
        btDeleteMail.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        btDeleteMail.setFocusable(false);
        btDeleteMail.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btDeleteMail.setPreferredSize(new java.awt.Dimension(40, 73));
        btDeleteMail.setSelectedIcon(new javax.swing.ImageIcon(getClass().getResource("/Save.png"))); // NOI18N
        btDeleteMail.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btDeleteMail.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btDeleteMailActionPerformed(evt);
            }
        });
        jToolBar1.add(btDeleteMail);

        btCalendar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Calendar.png"))); // NOI18N
        btCalendar.setMnemonic('1');
        btCalendar.setText("Calendar");
        btCalendar.setToolTipText("Manage Calendar and Task");
        btCalendar.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        btCalendar.setFocusable(false);
        btCalendar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btCalendar.setPreferredSize(new java.awt.Dimension(40, 73));
        btCalendar.setSelectedIcon(new javax.swing.ImageIcon(getClass().getResource("/Save.png"))); // NOI18N
        btCalendar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btCalendar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCalendarActionPerformed(evt);
            }
        });
        jToolBar1.add(btCalendar);

        btCalendar1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/paste.png"))); // NOI18N
        btCalendar1.setMnemonic('1');
        btCalendar1.setText("Tasks");
        btCalendar1.setToolTipText("Manage Calendar and Task");
        btCalendar1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        btCalendar1.setFocusable(false);
        btCalendar1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btCalendar1.setPreferredSize(new java.awt.Dimension(40, 73));
        btCalendar1.setSelectedIcon(new javax.swing.ImageIcon(getClass().getResource("/Save.png"))); // NOI18N
        btCalendar1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btCalendar1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCalendar1ActionPerformed(evt);
            }
        });
        jToolBar1.add(btCalendar1);

        jSplitPane1.setDividerLocation(180);
        jSplitPane1.setDividerSize(1);

        javax.swing.tree.DefaultMutableTreeNode treeNode1 = new javax.swing.tree.DefaultMutableTreeNode("Manager");
        javax.swing.tree.DefaultMutableTreeNode treeNode2 = new javax.swing.tree.DefaultMutableTreeNode("MailIn");
        javax.swing.tree.DefaultMutableTreeNode treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("Inbox");
        treeNode2.add(treeNode3);
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("Sent");
        treeNode2.add(treeNode3);
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("Unsent");
        treeNode2.add(treeNode3);
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("Draft");
        treeNode2.add(treeNode3);
        treeNode1.add(treeNode2);
        treeNode2 = new javax.swing.tree.DefaultMutableTreeNode("User Folder");
        treeNode1.add(treeNode2);
        trFolder.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1));
        trFolder.setDragEnabled(true);
        trFolder.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                trFolderMouseClicked(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                trFolderMouseReleased(evt);
            }
        });
        jScrollPane1.setViewportView(trFolder);

        jSplitPane1.setLeftComponent(jScrollPane1);

        jSplitPane2.setDividerLocation(200);
        jSplitPane2.setDividerSize(3);
        jSplitPane2.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);

        tbListMail.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Chủ đề", "Người gởi", "Ngày gởi", "Đính kèm"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                true, true, true, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tbListMail.setSelectionMode(javax.swing.ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        tbListMail.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbListMailMouseClicked(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                tbListMailMousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                tbListMailMouseReleased(evt);
            }
        });
        jScrollPane2.setViewportView(tbListMail);
        tbListMail.getColumnModel().getColumn(3).setResizable(false);

        org.jdesktop.layout.GroupLayout jPanel3Layout = new org.jdesktop.layout.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 680, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 199, Short.MAX_VALUE)
        );

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        jSplitPane2.setTopComponent(jPanel1);

        jSplitPane3.setDividerLocation(60);
        jSplitPane3.setDividerSize(3);
        jSplitPane3.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);

        jScrollPane3.setViewportView(txtContentMail);

        jSplitPane3.setRightComponent(jScrollPane3);

        org.jdesktop.layout.GroupLayout jPanel4Layout = new org.jdesktop.layout.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 659, Short.MAX_VALUE)
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 100, Short.MAX_VALUE)
        );

        jScrollPane4.setViewportView(jPanel4);

        jSplitPane3.setLeftComponent(jScrollPane4);

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jSplitPane3)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jSplitPane3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 247, Short.MAX_VALUE)
        );

        jSplitPane2.setRightComponent(jPanel2);

        jSplitPane1.setRightComponent(jSplitPane2);

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder("QuickSearch"));

        cbChooseSearch.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "By Subject", "By Address Sender" }));

        txtSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtSearchActionPerformed(evt);
            }
        });
        txtSearch.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtSearchKeyReleased(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel5Layout = new org.jdesktop.layout.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .add(cbChooseSearch, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 135, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(txtSearch, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 165, Short.MAX_VALUE))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel5Layout.createSequentialGroup()
                .add(jPanel5Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(cbChooseSearch, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(txtSearch, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(6, Short.MAX_VALUE))
        );

        jMenu1.setText("File");

        jMenu4.setText("New");

        mnNewMail.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
        mnNewMail.setText("New Mail");
        mnNewMail.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnNewMailActionPerformed(evt);
            }
        });
        jMenu4.add(mnNewMail);

        jMenu1.add(jMenu4);
        jMenu1.add(jSeparator3);

        mnExit.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F4, java.awt.event.InputEvent.ALT_MASK));
        mnExit.setText("Exit");
        mnExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnExitActionPerformed(evt);
            }
        });
        jMenu1.add(mnExit);
        jMenu1.add(jSeparator2);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Action");

        mnGetMail.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_1, java.awt.event.InputEvent.CTRL_MASK));
        mnGetMail.setText("Receive mail");
        mnGetMail.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnGetMailActionPerformed(evt);
            }
        });
        jMenu2.add(mnGetMail);

        mnWriteMail.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_2, java.awt.event.InputEvent.CTRL_MASK));
        mnWriteMail.setText("Write new mail");
        mnWriteMail.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnWriteMailActionPerformed(evt);
            }
        });
        jMenu2.add(mnWriteMail);

        mnReplyMail.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_3, java.awt.event.InputEvent.CTRL_MASK));
        mnReplyMail.setText("Reply mail");
        mnReplyMail.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnReplyMailActionPerformed(evt);
            }
        });
        jMenu2.add(mnReplyMail);

        mnContact.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_4, java.awt.event.InputEvent.CTRL_MASK));
        mnContact.setText("Contact");
        mnContact.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnContactActionPerformed(evt);
            }
        });
        jMenu2.add(mnContact);

        mnConfig.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_5, java.awt.event.InputEvent.CTRL_MASK));
        mnConfig.setText("Wizard Config");
        mnConfig.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnConfigActionPerformed(evt);
            }
        });
        jMenu2.add(mnConfig);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(jToolBar1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 498, Short.MAX_VALUE)
                .add(18, 18, 18)
                .add(jPanel5, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jSplitPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 864, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(jToolBar1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 57, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jPanel5, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 53, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(10, 10, 10)
                .add(jSplitPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 453, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btGetMailActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btGetMailActionPerformed
        // TODO add your handling code here:

            try {

                int IdDef = ReadXML.getIDDefaultUser();
                if (IdDef == -1)
                {
                    JOptionPane.showMessageDialog(rootPane, "The default Account does not set !! Please set to run process");
                    this.btConfigActionPerformed(evt);
                    return;
                }
                mailFuncBUS.showMails(tbListMail);
                mailFuncBUS.saveMail2Database(tbListMail);

            } catch (Exception ex) {
                Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
            }
    

    }//GEN-LAST:event_btGetMailActionPerformed

    private void tbListMailMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbListMailMouseClicked
        // TODO add your handling code here:
        rows = tbListMail.rowAtPoint(evt.getPoint());
        //JOptionPane.showMessageDialog(rootPane, rows);
        if ( evt.getClickCount() == 2)
        {
            MailStructureDTO mail = mailFuncBUS.getMail(tbListMail, rows);
            OpenMailDlg tmp = new OpenMailDlg(mail, rows, tbListMail);
            tmp.show();
            return;
        }


        if (rows>-1){
            
            txtContentMail.setEditable(false);
            String content = mailFuncBUS.getContentMail(txtContentMail,tbListMail, rows);

            txtContentMail.setText(content);
            txtContentMail.addHyperlinkListener(new MyHyperlinkListener());
         try {
                showAttachFile(rows);
            } catch (FileNotFoundException ex) {
                Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        

    }//GEN-LAST:event_tbListMailMouseClicked
     class MyHyperlinkListener implements HyperlinkListener {
        public void hyperlinkUpdate(HyperlinkEvent evt) {
            if (evt.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
                JEditorPane pane = (JEditorPane)evt.getSource();
                try {
                    // Show the new page in the editor pane.
                    pane.setPage(evt.getURL());
                } catch (IOException e) {
                }
            }
        }
    }


    private void  showAttachFile(int rows) throws FileNotFoundException{
        ArrayList<File> attachFile = mailFuncBUS.showAttachFile(rows, tbListMail);
         JPanel neo;
         GridLayout layout  = new GridLayout(0, 3);
          neo = new JPanel(layout);
        if (attachFile == null || attachFile.size()==0) {
            
//            attachFile = new ArrayList<File>();
//            for (int i = 0; i < 4; i++) {
//                attachFile.add(new File(Integer.toString(i)));
//            }

             neo.add(new JLabel(""));
             jScrollPane4.setViewportView(neo);
             return;

        }

        // panel.setPreferredSize(new Dimension(mainView.getWidth()-20,attachFile.size()*30));
        //jScrollPane4.getCo
        JLabel[] labels = new JLabel[attachFile.size()];
       // jScrollPane4.removeAll();
             
        jScrollPane4.setViewportView(neo);
      
        
        for (int index = 0; index < attachFile.size(); index++) {
            labels[index] = new JLabel();
            labels[index].setText(attachFile.get(index).getName());
            //ShellFolder sf;

//                sf = ShellFolder.getShellFolder(attachFile.get(index));
            Icon icon = new ImageIcon("Image\\bullet-red.png");
            labels[index].setIcon(icon);
//                labels[index].setToolTipText(attachFile.get(index).getPath());
            labels[index].addMouseListener(new MouseAdapter() {

                @Override
                public void mousePressed(MouseEvent evt) {
                    try {
                        jLabelMousePressed(evt);
                    } catch (Exception ex) {
                        Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                @Override
                public void mouseMoved(MouseEvent e) {
                    Cursor tmp = new Cursor( Cursor.HAND_CURSOR);
                    setCursor(tmp);

                }
               
            });
            neo.add(labels[index]);

        }
        neo.setLayout(layout);
        neo.doLayout();
    }
    private void jLabelMousePressed(MouseEvent evt) throws Exception {
        JLabel label = (JLabel) evt.getComponent();
        String time = label.getText();
        //JOptionPane.showMessageDialog(rootPane, "Xin chao" + time);
        JFileChooser chooserFolder = new JFileChooser();
        chooserFolder.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        if (chooserFolder.showSaveDialog(label) == (int) JFileChooser.APPROVE_OPTION) {
            String folder = chooserFolder.getSelectedFile().getPath();

            if (rows > -1) {
                mailFuncBUS.saveAttachFile(rows, tbListMail, time, folder);
            }
        }


    }
    private void tbListMailMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbListMailMousePressed
        // TODO add your handling code here:
        
        
    }//GEN-LAST:event_tbListMailMousePressed

    private void tbListMailMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbListMailMouseReleased
        // TODO add your handling code here:
        
    }//GEN-LAST:event_tbListMailMouseReleased
    private void showListBox(){
        
    }
    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        try {
            // TODO add your handling code here:
            //jscrMailMoreInfo.setVisible(false);
            mailInBus = new MailInBUS();
            listBox = mailInBus.getListBox();
            mailInBus.showListBox(trFolder, listBox);
        } catch (SQLException ex) {
            Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
        }
        showListBox();
        int idAcc ;
        try {
            idAcc = ReadXML.getIDDefaultUser();
             if ( idAcc == -1)
                JOptionPane.showMessageDialog(null,"The Default Account do not set !!!");
             else
                  try {
                mailFuncBUS = new MailFunctionBUS();
            } catch (Exception ex) {
                Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SAXException ex) {
            Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
        }

       
       
        add2PopupMenu();
//        menuItem.addActionListener(new ActionListener() {
//
//            public void actionPerformed(ActionEvent e) {
//               //nothing
//
//            }
//        });
        btMarkMail.addMouseListener(new MouseAdapter() {
             @Override
            public void mouseReleased(MouseEvent evt) {
                if (evt.isPopupTrigger()) {
                    Pmenu.show(evt.getComponent(), evt.getX(), evt.getY());
                }
            }
              @Override
            public void mouseClicked(MouseEvent evt){
                Pmenu.show(evt.getComponent(), evt.getX(), evt.getY());
                //}
            }
            
        });
        tbListMail.addMouseListener(new MouseAdapter(){
            @Override
            public void mouseReleased(MouseEvent evt) {
                if (evt.isPopupTrigger()) {
                    Pmenu.show(evt.getComponent(), evt.getX(), evt.getY());
                }
            }
           
            }
        );
        Pmenu2 = new JPopupMenu();
        JMenuItem menuItem = new JMenuItem("Add new Folder");
        menuItem.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                String nodeName = JOptionPane.showInputDialog("Name Of Folder");
                if ( nodeName == null){
                   
                    return;
                }
                if (nodeName.isEmpty()){
                     JOptionPane.showMessageDialog(rootPane, "Folder Name is not empty");
                     return;
                }
                try {
                    if (mailInBus.searchNode(nodeName)) {
                        JOptionPane.showMessageDialog(rootPane, "Folder Name already Exist");
                        return;
                    }
                } catch (SQLException ex) {
                    Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
                }
                try {
                    mailInBus.addNode(trFolder, nodeName);
                    add2PopupMenu();
                } catch (SQLException ex) {
                    Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });

        Pmenu2.add(menuItem);
         menuItem = new JMenuItem("ModifyFolder");
        menuItem.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                DefaultMutableTreeNode _nodeSelected = (DefaultMutableTreeNode) trFolder.getSelectionPath().getLastPathComponent();
               String name = _nodeSelected.toString();
               
                if (name.equals("Inbox") || name.equals("Sent") || name.equals("Unsend") || name.equals("Draft") || name.equals("User Folder") || name.equals("MainlIn") || name.equals("Manager")) {
                    JOptionPane.showMessageDialog(rootPane, "Can not modify Default Folder");
                    return;
                }
                MailInDTO modify = null;
                try {
                   modify = mailInBus.searchMailIn(name);
                } catch (SQLException ex) {
                    Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
                }
                String nodeName = "";
                nodeName = JOptionPane.showInputDialog("modify name Folder", name);
                if (nodeName == null) {
                    return;
                }
                if (nodeName.isEmpty()) {
                    JOptionPane.showMessageDialog(rootPane, "Folder Name is not empty");
                    return;
                }
                try {
                    if (mailInBus.searchNode(nodeName)) {
                        JOptionPane.showMessageDialog(rootPane, "Folder Name already Exist");
                        return;
                    }
                } catch (SQLException ex) {
                    Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
                }
                try {
                     //_nodeSelected = (DefaultMutableTreeNode) trFolder.getSelectionPath().getLastPathComponent();

                     //_nodeSelected
                if (nodeName.equals("Inbox") || nodeName.equals("Sent") || nodeName.equals("Unsend") || nodeName.equals("Draft") || nodeName.equals("User Folder") || nodeName.equals("MainlIn") || nodeName.equals("Manager")) {
                    JOptionPane.showMessageDialog(rootPane, "Can not modify Default Folder");
                    return;
                }

                    modify.setNameIn(nodeName);

                    mailInBus.modifyFolder(modify);
                    add2PopupMenu();
                     listBox = mailInBus.getListBox();
                    //mailInBus.reLoad(trFolder);
                     mailInBus.showListBox(trFolder, listBox);
                } catch (SQLException ex) {
                    Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });

        Pmenu2.add(menuItem);
         menuItem = new JMenuItem("Delete Folder");
        menuItem.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                //String nodeName = JOptionPane.showInputDialog("Name Of Folder");
               DefaultMutableTreeNode _nodeSelected = (DefaultMutableTreeNode) trFolder.getSelectionPath().getLastPathComponent();
               String name = _nodeSelected.toString();
                if (name.equals("Inbox") || name.equals("Sent") || name.equals("Unsend") || name.equals("Draft") || name.equals("User Folder") || name.equals("MainlIn") || name.equals("Manager")) {
                    JOptionPane.showMessageDialog(rootPane, "Can not delete Default Folder");
                    return;
                }
                try {
                    mailInBus.deleteNode(trFolder, _nodeSelected);
                     listBox = mailInBus.getListBox();
                    mailInBus.showListBox(trFolder, listBox);
                    add2PopupMenu();
                } catch (SQLException ex) {
                    Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
                }
               
            }
        });
        
        Pmenu2.add(menuItem);

         trFolder.addMouseListener(new MouseAdapter(){
            @Override
            public void mouseReleased(MouseEvent evt) {
                if (evt.isPopupTrigger()) {
                    Pmenu2.show(evt.getComponent(), evt.getX(), evt.getY());
                }
            }

            }
        );

    }//GEN-LAST:event_formWindowOpened

    private void btWriteMailActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btWriteMailActionPerformed
        // TODO add your handling code here:
        FrmSendMailGUI send = new FrmSendMailGUI();
        send.show();
    }//GEN-LAST:event_btWriteMailActionPerformed

    private void btConfigActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btConfigActionPerformed
//        try {
//        try {
//            // TODO add your handling code here:
//            showAttachFile(0);
//        } catch (FileNotFoundException ex) {""NgÆ°á»i
//            Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
//        }
//            String utf8 = ConvertString.ConvertString2UTF8("Bảo");
//            String re = ConvertString.ConvertUTF82String(utf8);
//            JOptionPane.showMessageDialog(rootPane, utf8 +"   NgÆ°á»i    " + re);

        //JOptionPane.showMessageDialog(rootPane, "dasdsad");
        //ConfigAccountGUI temp = new ConfigAccountGUI();
       ConfigAccGUI temp = new ConfigAccGUI();
       temp.show();

//        } catch (Exception ex) {
//            Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
//        }

       
    }//GEN-LAST:event_btConfigActionPerformed

    private void btContactActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btContactActionPerformed
        // TODO add your handling code here:
        ContactsGUI frmContact = null;
        try {
            frmContact = new ContactsGUI();
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
        }
        frmContact.show();

        

    }//GEN-LAST:event_btContactActionPerformed

    private void trFolderMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_trFolderMouseClicked
        // TODO add your handling code here:
     
    }//GEN-LAST:event_trFolderMouseClicked

    private void mnConfigActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnConfigActionPerformed
        // TODO add your handling code here:
       
        //ConfigAccountGUI temp = new ConfigAccountGUI();
        //ConfigAccountDlgGUI.setDefaultLocale(new Locale("English"));
        ConfigAccGUI showConfig = new ConfigAccGUI();
        showConfig.show();
    }//GEN-LAST:event_mnConfigActionPerformed

    private void trFolderMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_trFolderMouseReleased
        // TODO add your handling code here:
        int modifiers = evt.getModifiers();
         int idFolder = -1;
        if ((modifiers & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) {
          //System.out.println("Left button pressed.");
            DefaultMutableTreeNode _nodeSelected = (DefaultMutableTreeNode) trFolder.getSelectionPath().getLastPathComponent();
               String name = _nodeSelected.toString();
              
            try {
                idFolder = mailInBus.searchMailIn(name).getIdMainIn();
            } catch (SQLException ex) {
                Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                mailFuncBUS.showMails(tbListMail, idFolder);
            } catch (Exception ex) {
                Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_trFolderMouseReleased

    private void btMarkMailActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btMarkMailActionPerformed
        // TODO add your handling code here:
//        MailStructureDTO mail = mailFuncBUS.getMail(tbListMail, rows);
//        if ( rows>-1)
//        {
//            try {
//                mailFuncBUS.deleteMail(mail);
//            } catch (SQLException ex) {
//                Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
//            } catch (Exception ex) {
//                Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
//            }
//
//        }
    }//GEN-LAST:event_btMarkMailActionPerformed

    private void btDeleteMailActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btDeleteMailActionPerformed
        // TODO add your handling code here:
        MailStructureDTO mail = mailFuncBUS.getMail(tbListMail, rows);
        try {
            mailFuncBUS.deleteMail(mail);
            mailFuncBUS.showMails(tbListMail);
        } catch (SQLException ex) {
            Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btDeleteMailActionPerformed

    private void btCalendarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCalendarActionPerformed
        // TODO add your handling code here:
       
        try {
             CalendarGUI  calen = new CalendarGUI();
           calen.show();
        } catch (SQLException ex) {
            Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }//GEN-LAST:event_btCalendarActionPerformed
    /**
     * reply mail when click on mail
     * @param evt
     */
    private void btReplyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btReplyActionPerformed
        // TODO add your handling code here:
        FrmSendMailGUI send = new FrmSendMailGUI();
        MailStructureDTO mail = mailFuncBUS.getMail(tbListMail, rows);
        String mailSender = mail.getSender();
        if(mailSender.isEmpty())
        {
            JOptionPane.showMessageDialog(null, "Mail Receiver is not valid");
            return;
        }
        mailSender = StringFunctions.cutStringByChac(mailSender, "<").get(1);
        mailSender = StringFunctions.cutStringByChac(mailSender, ">").get(0);
        send.setSubject("RE: " + mail.getTitle());
        send.setReceiver(mailSender);
        send.show();
    }//GEN-LAST:event_btReplyActionPerformed

    private void txtSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtSearchActionPerformed
        try {
            // TODO add your handling code here:
            searchMailbyType(txtSearch.getText());
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SAXException ex) {
            Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
        }  catch (IOException ex) {
            Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
        }catch (Exception ex) {
            Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_txtSearchActionPerformed

    private void txtSearchKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtSearchKeyReleased
        // TODO add your handling code here:

    }//GEN-LAST:event_txtSearchKeyReleased

    private void mnExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnExitActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_mnExitActionPerformed

    private void mnNewMailActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnNewMailActionPerformed
        // TODO add your handling code here:
        this.btWriteMailActionPerformed(evt);
    }//GEN-LAST:event_mnNewMailActionPerformed

    private void mnWriteMailActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnWriteMailActionPerformed
        // TODO add your handling code here:
        this.btWriteMailActionPerformed(evt);
    }//GEN-LAST:event_mnWriteMailActionPerformed

    private void mnReplyMailActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnReplyMailActionPerformed
        // TODO add your handling code here:
        this.btReplyActionPerformed(evt);
    }//GEN-LAST:event_mnReplyMailActionPerformed

    private void mnContactActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnContactActionPerformed
        // TODO add your handling code here:
        this.btContactActionPerformed(evt);
    }//GEN-LAST:event_mnContactActionPerformed

    private void mnGetMailActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnGetMailActionPerformed
        // TODO add your handling code here:
        this.btGetMailActionPerformed(evt);
    }//GEN-LAST:event_mnGetMailActionPerformed

    private void btCalendar1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCalendar1ActionPerformed
        // TODO add your handling code here:
        TaskGUI taskfrm = new TaskGUI();
        taskfrm.show();
    }//GEN-LAST:event_btCalendar1ActionPerformed
 private int searchMailbyType( String nameSearch) throws ParserConfigurationException, SAXException, SAXException, IOException, IOException, Exception{
     int index = -1;
     DefaultMutableTreeNode _nodeSelected = (DefaultMutableTreeNode) trFolder.getSelectionPath().getLastPathComponent();
     String name = _nodeSelected.toString();
    
     try {
         int idFolder = mailInBus.searchMailIn(name).getIdMainIn();
         ArrayList<MailStructureDTO> lstMail = mailFuncBUS.getListMail(idFolder);
         ArrayList<MailStructureDTO> searchMail = new ArrayList<MailStructureDTO>();
         for (MailStructureDTO mail : lstMail) {
             switch ( cbChooseSearch.getSelectedIndex()){
                 case 1:
                     if ( mail.getSender().contains(nameSearch))
                         searchMail.add(mail);
                     break;
                 case 0:
                     if(mail.getTitle().contains(nameSearch))
                          searchMail.add(mail);
                     break;
             }
         }
         mailFuncBUS.showSearchMails(tbListMail, searchMail);
     } catch (SQLException ex) {
         Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
     }
    
     return index;
 }
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MainApp().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btCalendar;
    private javax.swing.JButton btCalendar1;
    private javax.swing.JButton btConfig;
    private javax.swing.JButton btContact;
    private javax.swing.JButton btDeleteMail;
    private javax.swing.JButton btGetMail;
    private javax.swing.JButton btMarkMail;
    private javax.swing.JButton btReply;
    private javax.swing.JButton btWriteMail;
    private javax.swing.JComboBox cbChooseSearch;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JToolBar.Separator jSeparator4;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JSplitPane jSplitPane2;
    private javax.swing.JSplitPane jSplitPane3;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JMenuItem mnConfig;
    private javax.swing.JMenuItem mnContact;
    private javax.swing.JMenuItem mnExit;
    private javax.swing.JMenuItem mnGetMail;
    private javax.swing.JMenuItem mnNewMail;
    private javax.swing.JMenuItem mnReplyMail;
    private javax.swing.JMenuItem mnWriteMail;
    private javax.swing.JTable tbListMail;
    private javax.swing.JTree trFolder;
    private javax.swing.JEditorPane txtContentMail;
    private javax.swing.JTextField txtSearch;
    // End of variables declaration//GEN-END:variables

}
